﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using DataService;
public partial class Users_accountEditUser : BasePage
{
    private UserService _userService;
    public Users_accountEditUser()
    {
        _userService = new UserService();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["u"]) && Session["EditUser"] != null)
        {
            lbInformation.Text = Convert.ToString(Request.QueryString["u"]);
        }
        else
        {
            Response.Redirect("Default.aspx");
        }

        User user = (User)Session["EditUser"];

        if (!IsPostBack)
        {
            rptRole.DataSource = _userService.GetRoles(user);
            rptServerRole.DataSource = _userService.GetServerRoles(user);
            DataBind();
            ((TextBox)txtFirstName).Text = user.FirstName;
            ((TextBox)txtLastName).Text = user.LastName;
            ((TextBox)txtEmail).Text = user.Email;
            ((TextBox)txtPhone).Text = user.Phone;
        }

        //ContactSubmit1.EditUser = user;

        //Control divUserLogin = ContactSubmit1.FindControl("divUserLogin");
        //if (divUserLogin != null) divUserLogin.Visible = false;

        //Control divContact = ContactSubmit1.FindControl("divContact");
        //if (divContact != null) divContact.Visible = false;

        //Control divBillingType = ContactSubmit1.FindControl("divBillingType");
        //if (divBillingType != null) divBillingType.Visible = false;

        //Control divSentMail = ContactSubmit1.FindControl("divSentMail");
        //if (divSentMail != null) divSentMail.Visible = false;




        
        //Control txtFirstName = ContactSubmit1.FindControl("txtFirstName");
        //if (txtFirstName != null)
        //    ((TextBox)txtFirstName).Text = user.FirstName;

        //Control txtLastName = ContactSubmit1.FindControl("txtLastName");
        //if (txtLastName != null)
        //    ((TextBox)txtLastName).Text = user.LastName;

        //Control txtEmail = ContactSubmit1.FindControl("txtEmail");
        //if (txtEmail != null)
        //    ((TextBox)txtEmail).Text = user.Email;

        //Control txtPhone = ContactSubmit1.FindControl("txtPhone");
        //if (txtPhone != null)
        //    ((TextBox)txtPhone).Text = user.Phone;

        

        if (user.IsActived)
        {
            lbtnDisableUser.Text = "Disable User";
        }
        else
        {
            lbtnDisableUser.Text = "Enable User";
        }

    }
    protected void lbtnDisableUser_Click(object sender, EventArgs e)
    {

        User user = (User)Session["EditUser"];
        _userService.SetActiveUser(user.Id, !user.IsActived);
        user.IsActived = !user.IsActived;
        Session["EditUser"] = user;
        //Response.Redirect("editUser.aspx?u="+user.Username);
        Response.Redirect("Default.aspx");
    }
    protected void btSave_Click(object sender, EventArgs e)
    {
        // TextBox txtFirstName =(TextBox) ContactSubmit1.FindControl("txtFirstName");

        //TextBox txtLastName = (TextBox)ContactSubmit1.FindControl("txtLastName");

        //TextBox txtEmail = (TextBox)ContactSubmit1.FindControl("txtEmail");

        //TextBox txtPhone =(TextBox) ContactSubmit1.FindControl("txtPhone");


        User user = (User)Session["EditUser"];
        _userService.InsertUpdateUserInfo(UserLogin.Customer.Id, user.Id, user.Username, user.Password, txtFirstName.Text, txtLastName.Text, txtEmail.Text, txtPhone.Text);

        //Repeater rptRole = (Repeater)ContactSubmit1.FindControl("rptRole");
        foreach (RepeaterItem item in rptRole.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("chkRole");
            HiddenField hdn = (HiddenField)item.FindControl("hdnRoleId");
            _userService.InsertUpdateUserRoleAssociation(user.Id, Convert.ToInt32(hdn.Value), chk.Checked);
        }
        //Repeater rptServerRole = (Repeater)ContactSubmit1.FindControl("rptServerRole");
        foreach (RepeaterItem item in rptServerRole.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("chkServerRole");
            HiddenField hdn = (HiddenField)item.FindControl("hdnRoleId");
            _userService.InsertUpdateUserRoleAssociation(user.Id, Convert.ToInt32(hdn.Value), chk.Checked);
        }

        Response.Redirect("Default.aspx");
    }

}